﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MuaChung.Models;
using MuaChung.Base;
using System.Web.Security;

namespace MuaChung.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        [Authorize(Roles="Admin")]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        public ActionResult CouponPanel()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        public ActionResult ListAllAccount(int currentPage = 1, int pageSize = 10)
        {            
            ViewData["cp"] = currentPage;
            ViewData["ps"] = pageSize;
            List<TAI_KHOAN> model = AccountModel.GetAll(currentPage, pageSize);

            return View(model);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult DeleteAccount(int id)
        {
            TAI_KHOAN model = AccountModel.FindByID(id);
            if (model.SignedIn == 1)
            {
                ViewData["status"] = "SignedIn";
                return View(model);
            }
            if (AccountModel.DeleteAccount(id))
            {
                ViewData["status"] = "OK";
            }
            else
            {
                ViewData["status"] = "Fail";
            }
            return View(model);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult EditAccount(int id)
        {
            AccDetailModel model = new AccDetailModel();
            
            TAI_KHOAN tk = AccountModel.FindByID(id);
            CHITIET_TAIKHOAN cttk = AccountModel.FindDetail(tk.Username);

            model.CMND = cttk.CMND;
            model.DiaChi = cttk.DiaChi;
            model.Email = tk.Email;
            model.HoTen = cttk.HoTen;
            model.ngayDK = cttk.NgayDK;
            model.NgaySinh = cttk.NgaySinh;
            model.SDT = cttk.DienThoai;
            model.status = tk.Status;
            model.uid = tk.ID;
            model.UserName = tk.Username;

            return View(model);           
        }

        [HttpPost]
        [Authorize(Roles="Admin")]
        public ActionResult EditAccount(AccDetailModel model, FormCollection frm)
        {
            model.role = frm.GetValue("role").AttemptedValue;
            model.status = frm.GetValue("status").AttemptedValue;
            if (AccountModel.UpdateAccountInfo(model))
            {
                ViewData["Message"] = "Lưu thay đổi thành công!";
            }
            else
            {
                ViewData["Message"] = "Lưu thay đổi thất bại!";
            }
            return View(model);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult AddAccount()
        {
            AccDetailModel model = new AccDetailModel();

            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult AddAccount(AccDetailModel model,FormCollection frm)
        {
            TAI_KHOAN tk = new TAI_KHOAN();
            CHITIET_TAIKHOAN cttk = new CHITIET_TAIKHOAN();
            tk.Username = model.UserName;            
            tk.Password = frm.GetValue("password").AttemptedValue;
            tk.Role = frm.GetValue("role").AttemptedValue;
            tk.Status = frm.GetValue("status").AttemptedValue;
            tk.SignedIn = 0;
            tk.Email = model.Email;

            cttk.CMND = model.CMND;
            cttk.DiaChi = model.DiaChi;
            cttk.DienThoai = model.SDT;
            cttk.HoTen = model.HoTen;
            cttk.Ma_KichHoat = BaseWeb.GenerateRandomCode(10);
            cttk.NgayDK = DateTime.Now;
            cttk.NgaySinh = model.NgaySinh;

            MembershipCreateStatus status = AccountModel.AddUser(tk, cttk);
            if (status == MembershipCreateStatus.Success)
            {
                ViewData["Message"] = "Tạo tài khoản thành công.";
            }
            else
            {
                ViewData["Message"] = AccountValidation.ErrorCodeToString(status);
            }
            return View(model);
        }        

        [Authorize(Roles = "Admin")]
        public ActionResult ListAllCoupon(int currentPage = 1, int orderType = 1)
        {
            List<CouponDetailMin> model = new List<CouponDetailMin>();
            int pageSize = 10;
            List<COUPON> tmplst = CouponModel.GetAll(currentPage, pageSize, orderType);
            ViewData["cp"] = currentPage;
            ViewData["ps"] = pageSize;
            ViewData["ot"] = orderType;
            if (tmplst == null)
            {
                ViewData["status"] = "NoItem";
                return View(model);
            }
            foreach (var item in tmplst)
            {
                CouponDetailMin tmp = CouponModel.ToCouponDetailMin(item);
                model.Add(tmp);
            }
            ViewData["status"] = "OK";
            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult ListAllCoupon(FormCollection frm)
        {
            int currentPage = Convert.ToInt32(frm.GetValue("currentPage").AttemptedValue);
            int orderType = Convert.ToInt32(frm.GetValue("orderType").AttemptedValue);

            for (int i = 2; i < frm.AllKeys.Count()-1; i++)
            {
                int cid = Convert.ToInt32(frm.AllKeys[i]);
                switch (frm.AllKeys[frm.AllKeys.Count()-1])
                {
                    case "Update":
                        CouponModel.UpdateCouponFromWebservice(cid);
                        break;
                    case "Delete":
                        COUPON tmp = CouponModel.FindByID(cid);
                        CouponModel.DeleteCoupon(tmp);
                        break;
                    default:
                        break;
                }
            }

            List<CouponDetailMin> model = new List<CouponDetailMin>();
            int pageSize = 10;
            List<COUPON> tmplst = CouponModel.GetAll(currentPage, pageSize, orderType);
            ViewData["cp"] = currentPage;
            ViewData["ps"] = pageSize;
            ViewData["ot"] = orderType;
            if (tmplst == null)
            {
                ViewData["status"] = "NoItem";
                return View(model);
            }
            foreach (var item in tmplst)
            {
                CouponDetailMin tmp = CouponModel.ToCouponDetailMin(item);
                model.Add(tmp);
            }
            ViewData["status"] = "OK";
            return View(model);
        }
        public ActionResult CreateCoupon()
        {
            CouponDetail model = new CouponDetail();

            return View(model);
        }

        [HttpPost]
        public ActionResult CreateCoupon(CouponDetail model, FormCollection frm)
        {
            COUPON cp = new COUPON();
            CHITIET_COUPON ctcp = new CHITIET_COUPON();
            cp.Ma_Coupon = model.maCoupon;
            cp.Ten_SanPham = model.tenSP;
            cp.SoLuong_CanMua = model.soLuongCanMua;
            cp.SoLuong_DaDat = 0;
            cp.SoLuong_Max = model.soLuongCoupon;
            cp.ThoiGian_BatDau = model.tgBatDau;
            cp.ThoiGian_KetThuc = model.tgKetThuc;
            cp.ThoiGian_DuocSuDung = model.tgDuocSuDung;
            cp.ThoiGian_HetHan = model.tgHetHan;
            cp.TyLeGiam = Convert.ToInt32((Convert.ToInt32(model.giaGoc) - Convert.ToInt32(model.giaTot)) / Convert.ToInt32(model.giaGoc) * 100);
            cp.Gia_Tot = model.giaTot;
            cp.Gia_Goc = model.giaGoc;
            cp.Ma_DoanhNghiep = DoanhNghiepModel.GetAll().First().ID;
            cp.Ma_Webservice = ServiceModel.GetAllWS().First().ID;
            ctcp.MoTa = model.moTa;
            ctcp.MoTa_TomTat = model.moTaTomTat;

            if (model.maCoupon == null || model.tenSP == null)
            {
                ViewData["message"] = "Bạn cần điền đầy đủ thông tin";
            }
            string mota = frm.GetValue("ctl00$MainContent$moTa").AttemptedValue;
            CouponModel.AddCoupon(cp, ctcp);
            return View(model);
        }
    }
}
